package com.cs.vessel.breakdown.wizards;

import java.util.UUID;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.sc.vessel.info.editor.InfoEditor;
import com.sc.vessel.info.editor.InfoInput;
import com.sc.vessel.info.model.breakdown.Accident;
import com.sc.vessel.info.model.breakdown.BreakdownFactory;

public class AccidentNewWizard extends Wizard implements INewWizard {

    private AccidentWizardFirstPage firstPage;

    private AccidentWizardSecondPage secondPage;

    private IStructuredSelection selection;

    private IWorkbench workbench;

    private Accident accident;

    public AccidentNewWizard() {
        accident = BreakdownFactory.eINSTANCE.createAccident();
        accident.setItemId(UUID.randomUUID().toString());
    }

    @Override
    public void init(IWorkbench workbench, IStructuredSelection selection) {
        this.workbench = workbench;
        this.selection = selection;
    }

    @Override
    public boolean performFinish() {
        InfoInput infoInput = new InfoInput(accident);
        IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
        try {
            page.openEditor(infoInput, InfoEditor.ID);
        } catch (PartInitException e) {
            System.err.println(e.getMessage());
        }
        return true;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.Wizard#addPages()
     */
    @Override
    public void addPages() {
        firstPage = new AccidentWizardFirstPage(Messages.ACCIDENTNEWWIZARD_CHOOSE_COMPONENT);
        secondPage = new AccidentWizardSecondPage(""); //$NON-NLS-1$
        addPage(firstPage);
        addPage(secondPage);
    }

    /**
     * Getter for firstPage.
     * 
     * @return the firstPage
     */
    public AccidentWizardFirstPage getFirstPage() {
        return this.firstPage;
    }

    /**
     * Getter for selection.
     * 
     * @return the selection
     */
    public IStructuredSelection getSelection() {
        return this.selection;
    }

    /**
     * Getter for workbench.
     * 
     * @return the workbench
     */
    public IWorkbench getWorkbench() {
        return this.workbench;
    }

    /**
     * Getter for accident.
     * 
     * @return the accident
     */
    public Accident getAccident() {
        return this.accident;
    }

}
